package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    //添加床位
    @Override
    public void save(BedDto bedDto) {
        //1、转成Bean
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //2、添加参数
        bed.setBedStatus(0);
        bedMapper.save(bed);
    }

    //根据ID查询床位
    @Override
    public BedVo findById(Long id) {
        return bedMapper.findById(id);
    }

    //修改床位
    @Override
    public void updateBed(BedDto bedDto) {
        //1、转为entity
        Bed bed = BeanUtil.toBean(bedDto,Bed.class);

        bedMapper.update(bed);
    }

    //删除床位
    @Override
    public void deleteById(Long id) {
        //查询床位状态
        BedVo bedVo = findById(id);
        if (bedVo.getBedStatus()!=0) {
            throw new BaseException(BasicEnum.ELDER_ALREADY_CHECKIN);
        }
        bedMapper.deleteById(id);
    }

}

